<!--
* description:
-->
<template>
  <div class="app-container app-container-table">
    <one-table-template
      v-if="initTemplate"
      ref="multipleTable"
      :dynamicButtons="tableButtons"
      :dynamicComponents="tableComponents"
      :dynamicApiConfig="apiConfig"
      :dynamicTableCols="tableCols"
      :dynamicFormFields="formField"
      :dynamicUnFormFields="unFormField"
      :dynamicIsShowSelect="false"
      :dynamicIsColumnDrop="true"
      :callbackObj="callbackObj"
    >
          <!-- :dynamicIsShowSelect="false" -->
          <!-- 复选框显示与隐藏 -->
      <template slot="controlBtn" slot-scope="scope">
        <el-link
          type="primary"
          class="el-icon-document"
          :underline="false"
          @click="detail(scope.row)"
          title="明细"
          style="font-size:12px;"
          >明细</el-link
        >
      </template>
      <template slot="DELIVERY_TIME" slot-scope="scope">
        <el-date-picker
          v-model="scope.row.DELIVERY_TIME"
          clearable
          value-format="yyyy-MM-dd"
          :editable="false"
          :unlink-panels="true"
          placeholder="请选择预计交货时间"
          style="width:180px;"
          class="tableTemplate"
          :picker-options="pickerOptions"
          v-if="scope.row.AUDIT_STATUS === '20'"
        />
        <span v-else>{{ scope.row.DELIVERY_TIME }}</span>
      </template>
    </one-table-template>
    <detail
      v-if="isShow"
      :rowData="rowData"
      @detailClose="detailClose"
      :isShow="isShow"
    />
    <mainDetail
      v-if="mainIsShow"
      @detailClose="mainDetailClose"
      :isShow="mainIsShow"
      :mainFormField="formField"
      :mainDetailType="mainDetailType"
    />
      <!-- <print v-if="isShowprint" :formField="formField" @detailClose="detailClose2" :isShow="isShowprint" /> -->
  </div>
</template>
<script>
import { oneTableWithViewTemplateMixins } from "@/components/mixins/oneTableWithViewTemplateMixins";
import { wmsApis } from "@/api/wms/purchase/api";
import OneTableTemplate from "@/components/wms/templates/oneTable";
import request from "@/utils/request";
import store from "@/store";
export default {
  name: "purchaseSupplierConfirm",
  // 组件混入对象
  mixins: [oneTableWithViewTemplateMixins],
  components: {
    OneTableTemplate,
    detail: () => import("./detail"),
    mainDetail: () => import("./mainDetail"),
     dialogHeader: () => import("@/components/dialogHeader"), //弹窗标题
    // print: () => import("./print")
  },
  watch: {
    $route(val) {
      if (
        this.formField.pageId !== "" &&
        this.$route.query.pageId !== undefined &&
        this.$route.query.pageId !== this.formField.pageId
      ) {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
        this.queryTable();
      }
    }
  },
  created() {
    if (this.$route.query != null) {
      // 判断传入query参数，初始化数据
      if (this.$route.query.pageId != null && this.$route.query.pageId !== "") {
        this.formField.pageId = this.$route.query.pageId;
        this.formField.pageName = this.$route.query.pageName;
      }
    }
  },
  mounted() {
    request({
      url: "/mp/wm/vl/basedata/partner/querySuppByUser.do",
      method: "POST",
      data: {}
    }).then(res => {
      if (res.listRow && res.listRow.length > 0) {
        if (res.listRow[0].PARTNER_ID) {
          this.formField.PARTNER_ID = res.listRow[0].PARTNER_ID;
          this.initTemplate = true;
          this.$nextTick(() => {
            this.queryTable();
          });
          this.querySelect();
        }
      } else {
        this.$message({
          message: "该用户未维护供应商信息，无法访问采购供应商确认",
          uration: 2000
        });
      }
    });
  },
  data() {
    return {
      // 打印弹框的显示与隐藏
      isShowprint:false,
         Before:'', //备件编号  处理前不加逗号的
      // 网格查询API配置对象
      apiConfig: wmsApis.queryPurchaseConfirm,
      // 动态组件-按钮
      tableButtons: [
        //  {
        //   compKey: "btnKey1",
        //   type: "primary",
        //   size: "small",
        //   clickEvent: () => this.print(),
        //   position: "right",
        //   text: "打印" //""
        // },
        {
          compKey: "btnKey2",
          type: "primary",
          size: "small",
          clickEvent: () => this.queryTable(),
          name: "search",
          position: "right",
          text: "查询" //"查询"
        },
        {
          compKey: "btnKey3",
          type: "",
          size: "small",
          name: "reset",
          position: "right",
          clickEvent: () => this.reset(),
          text: "重置"
        },
        // {
        //   compKey: "btnKey3",
        //   type: "",
        //   size: "small",
        //   clickEvent: () => this.confirm(),
        //   name: "confirm",
        //   position: "left",
        //   text: "确认"
        // },
        {
          compKey: "btnKey4",
          type: "",
          size: "small",
          clickEvent: () => this.mainDetail("detailed"),
          name: "detailed",
          position: "left",
          text: "发货"
        },
        {
          compKey: "btnKey5",
          type: "",
          size: "small",
          clickEvent: () => this.mainDetail("export"),
          
          name: "export",
          position: "left",
          text: "发货导出"
        },
         {
          compKey: "btnKey6",
          type: "",
          size: "small",
          clickEvent: () => this.export(),
          name: "export",
          position: "left",
          text: "订单导出", //"导出"
        },
      ],
      // 动态组件-查询条件
      tableComponents: [
        {
          compKey: "compKey1",
          labelName: "到货仓库",
          codeField: "WAREHOUSE_IDS",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: true,
          options: []
        },
         {
          compKey: "compKey2",
          labelName: "备件编号",
          codeField: "PART_NOS_NEW", //['0','9]
          component: () => import("@/components/org/commonInput/textareaInput"),
          type: "inputText",
          isMust: true,
        },
        {
          compKey: "compKey3",
          labelName: "采购类型",
          codeField: "PURCHASE_TYPE",
          component: () => import("@/components/wms/dropdownList/dropdownList"),
          type: "dropdownList",
          isMust: true,
          isMul: false,
          options: []
        },
        {
          compKey: "compKey4",
          labelName: "采购单号",
          codeField: "PURCHASE_CODE",
          component: () => import("@/components/wms/commonInput"),
          type: "inputText",
          isMust: true
        },
        // {
        //   compKey: "compKey5",
        //   labelName: "审核状态",
        //   codeField: "AUDIT_STATUS",
        //   component: () => import("@/components/wms/dropdownList/dropdownList"),
        //   type: "dropdownList",
        //   isMust: true,
        //   isMul: false,
        //   options: []
        // },
        {
          compKey: "compKey6",
          labelName: "要求到货时间",
          codeField: "RA_TIME_START",
          component: () => import("@/components/wms/datePicker/andtDatePicker"),
          type: "date",
          isMust: true
        }
      ],
      // 动态生成网格列
      tableCols: [
        {
          prop: "controlBtn",
          label: "操作",
          codeField: "controlBtn",
          width: 80,
          align: "center",
          isSlot: true
        },
        {
          prop: "PURCHASE_CODE",
          label: "采购单号",
          align: "center"
        },
        {
          prop: "SUPPLIER_CODE",
          label: "供应商编号",
          align: "center"
        },
        
        {
          prop: "PARTNER_SHORT_NAME",
          label: "供应商简称",
          align: "center"
        },
        {
          prop: "PURCHASE_TYPE_NAME",
          label: "采购类型",
          align: "center"
        },
        {
          prop: "WAREHOUSE_NAME",
          label: "到货仓库",
          align: "center"
        },
        {
          prop: "RA_TIME_NEW",
          label: "到货要求时间",
          align: "center"
        },
        // {
        //   prop: "DELIVERY_TIME",
        //   label: "预计交货时间+",
        //   codeField: "DELIVERY_TIME",
        //   width: 200,
        //   align: "center",
        //   isSlot: true,
        //   overflow: false
        // },
        // {
        //   prop: "AUDIT_STATUS_NAME",
        //   label: "审核状态",
        //   align: "center"
        // },
        {
          prop: "UPDATE_CONTROL_ID",
          label: "UPDATE_CONTROL_ID",
          align: "center",
          hidden: true
        }
      ],
      //表单查询数据
      formField: {
        WAREHOUSE_IDS: "",
        PART_NO: "",
        PURCHASE_TYPE: "",
        PURCHASE_CODE: "",
        RA_TIME_START: "",
        PARTNER_ID: "",
        AUDIT_STATUS: "",
        PART_NOS_NEW:""
      },
      unFormField: {},

      // 控制明细弹窗
      isShow: false,
      // 行数据
      rowData: {},
      // 控制主弹窗
      mainIsShow: false,
      mainDetailType: "",
      // 回调函数
      callbackObj: {
        //  当选择项发生变化时会触发该事件
        onCheckSelectChange: selection => {
          this.checkTableList = selection;
        },
         onBeforeQuery: queryObj => {
           queryObj.variables.PART_NOS_NEW = queryObj.variables.PART_NOS_NEW.trim()
            .split("\n")
            .toString();
        },
      },
      // 选中的表格数据
      checkTableList: [],
      // 控制初始化页面
      initTemplate: false,
      pickerOptions: {
        disabledDate: function(time) {
          return time.getTime() < Date.now() - 8.64e7;
        }
      }
    };
  },
  methods: {
    // 打印方法
    // print () {
    //   this.isShowprint = true
    // },
    // 点击审核按钮
    // confirm() {
    //   if (this.checkTableList.length > 0) {
    //     this.$confirm("确认要执行此操作吗？", "提示", {
    //       confirmButtonText: "确认",
    //       cancelButtonText: "取消"
    //     })
    //       .then(() => {
    //         request({
    //           url: "/mp/wm/vl/warehouse/purchase/purchaseConfirm.do",
    //           method: "post",
    //           data: {
    //             Confirm: this.checkTableList
    //           }
    //         }).then(res => {
    //           if (res.result === "1") {
    //             this.queryTable();
    //             this.$message({
    //               message: "确认成功",
    //               type: "success"
    //             });
    //           } else {
    //             this.$message({
    //               message: res.debugMsg ? res.debugMsg : res.msg,
    //               type: "warn"
    //             });
    //           }
    //         });
    //       })
    //       .catch(err => {
    //         console.info(err);
    //       });
    //   } else {
    //     this.$message({
    //       message: "请选择一条数据"
    //     });
    //   }
    // },
    detail(row) {
      this.rowData = row;
      // console.log(this.rowData);
      this.isShow = true;
    },
    detailClose(isRefresh) {
      this.isShow = false;
      if (isRefresh) {
        this.queryTable();
      }
    },
    detailClose2() {
      this.isShowprint = false;
    },
    mainDetail(type) {
      this.mainDetailType = type;
      this.mainIsShow = true;
    },
    mainDetailClose() {
      this.mainIsShow = false;
    },
    querySelect() {
      // 加载遮罩  暂不使用
      // store.dispatch("app/loading", true);
      request({
        url: "mp/wm/vl/warehouse/purchase/queryWareHouseByPurc.do",
        method: "post",
        data: {
          PARTNER_ID: this.formField.PARTNER_ID
        }
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[0].options.push({
              label: item.WAREHOUSE_NAME,
              value: item.WAREHOUSE_ID
            });
          });
        }
      });
      request({
        url: "/mp/wm/vl/warehouse/purchase/queryPurchaseTypeBox.do",
        method: "post",
        data: {}
      }).then(res => {
        if (res.listRow && res.listRow.length > 0) {
          res.listRow.map(item => {
            this.tableComponents[2].options.push({
              label: item.PURCHASE_TYPE_NAME,
              value: item.PURCHASE_TYPE
            });
          });
        }
      });
      // request({
      //   url: "/mp/wm/vl/warehouse/purchase/queryPurchaseAuditStatusBox.do",
      //   method: "post",
      //   data: {}
      // }).then(res => {
      //   if (res.listRow && res.listRow.length > 0) {
      //     res.listRow.map(item => {
      //       if (item.AUDIT_STATUS === "20" || item.AUDIT_STATUS === "40") {
      //         this.tableComponents[4].options.push({
      //           label: item.AUDIT_STATUS_NAME,
      //           value: item.AUDIT_STATUS
      //         });
      //       }
      //     });
      //   }
      // });
    },
      export() {

        let that = this
      that.Before = this.formField.PART_NOS_NEW;
      that.formField.PART_NOS_NEW = this.formField.PART_NOS_NEW.trim().split("\n").toString();

      const exportLoading = this.$loading({
        lock: true,
        text: "正在导出...",
        // spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)",
        fullscreen: true,
      });
      request({
        url: "/mp/wm/vl/warehouse/purchase/exportToPurchase.do",
        method: "post",
        headers: {
          "Cache-Control": "no-cache, no-store,max-age=0, must-revalidate",
          Pragma: "no-cache",
          Expires: "0",
          "Content-Type": "application/json;charset=UTF-8",
        },
        responseType: "blob",
        data: this.formField,
      })
        .then((response) => {
          exportLoading.close();
          let blob = new Blob([response], { type: "application/vnd.ms-excel" }); // 这里表示xmlx类型
          let downloadElement = document.createElement("a");
          let href = window.URL.createObjectURL(blob); // 创建下载的链接
          downloadElement.href = href;
          downloadElement.download = "采购供应商订单导出.xls"; // 下载后文件名
          document.body.appendChild(downloadElement);
          downloadElement.click(); // 点击下载
          document.body.removeChild(downloadElement); // 下载完成移除元素
          window.URL.revokeObjectURL(href); // 释放掉blob对象
        })
        .then(response =>{
        this.formField.PART_NOS_NEW = this.Before;
        })
        .catch((err) => {
          exportLoading.close();
          console.log(err);
        });

    },
  }
};
</script>

<style type="text/css" lang="scss" scoped>
/deep/ .el-table .el-input__inner {
  border: none;
}
</style>
